﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Xml;

public partial class Manage_EditPower : System.Web.UI.Page
{
    private List<Topic> topics;
    private List<Power> powers;

    protected string powerTable = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string uid = Request.Params["uid"];

            CmsDataContext db = new CmsDataContext();
            topics = (from aT in db.Topic
                      select aT).ToList();
            powers = (from aP in db.Power
                      where aP.ManagerID == uid
                      select aP).ToList();

            powerTable = GeneratePowers(null, 0);

            if (powers.Where(aP => aP.Authority == (int)Powers.SystemAdministrator).Count() > 0)
            {
                OtherPowers.Items[0].Selected = true;
            }
        }
    }

    protected string GeneratePowers(string id, int level)
    {
        var ts = topics.Where(aT => aT.ParentID == id).OrderBy(aT => aT.ID);
        StringBuilder sb = new StringBuilder();

        string format = "<tr><td align=\"left\" style=\"padding-left: {0}px;\">{1}</td><td align=\"center\"><input type=\"checkbox\" id=\"chk_{2}_1\" name=\"power\" value=\"2\" {3}/><label for=\"chk_{2}_1\">文章发布</label>&nbsp;&nbsp;<input type=\"checkbox\" id=\"chk_{2}_2\" name=\"power\" value=\"4\" {4}/><label for=\"chk_{2}_2\">文章审核</label></td></tr>";

        foreach (Topic t in ts)
        {
            string s3 = "";
            string s4 = "";

            if (powers.Where(aP => aP.TopicID == t.ID && aP.Authority == (int)Powers.ArticleManager).Count() > 0)
            {
                s3 = "checked=\"checked\"";
            }
            if (powers.Where(aP => aP.TopicID == t.ID && aP.Authority == (int)Powers.ArticleVerfier).Count() > 0)
            {
                s4 = "checked=\"checked\"";
            }

            sb.Append(string.Format(format, (level * 10 + 5), t.Name, t.ID, s3, s4));
            sb.Append(GeneratePowers(t.ID, level + 1));
        }

        return sb.ToString();
    }
    protected void Save_Click(object sender, EventArgs e)
    {
        string val = ValueXML.Value.Trim().Replace("&lt;", "<").Replace("&gt;", ">");
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(val);
        
        CmsDataContext db = new CmsDataContext();
        string mid = Convert.ToString(doc.DocumentElement.Attributes["mid"].Value);
        try
        {
            #region 先删除用户当前的所有权限
            var q = from aP in db.Power
                    where aP.ManagerID == mid
                    select aP;
            db.Power.DeleteAllOnSubmit(q);
            #endregion

            #region 将权限写入数据库
            foreach (XmlNode n in doc.DocumentElement.SelectNodes("power")) {
                Power p = new Power();
                p.ID = Toolkit.NextUUID();
                p.ManagerID = mid;
                p.TopicID = n.Attributes["topicid"] == null ? null : Convert.ToString(n.Attributes["topicid"].Value);
                p.Authority = Convert.ToInt32(n.Attributes["value"].Value);
                p.Description = "";

                db.Power.InsertOnSubmit(p);
            }
            #endregion

            db.SubmitChanges();

            MessageBox.Message = "用户权限保存成功。";
        }
        catch
        {
            MessageBox.Message = "用户权限保存时发生错误。";
        }

        MessageBox.Location = "./EditPower.aspx?uid=" + mid;
    }
}